﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using StudentsInfor.Model;

namespace StudentsInfor.Services
{
    /// <summary>
    /// 读入Excel数据
    /// </summary>
    public class ReadExcel
    {
        public static void ReadExl(string path ,List<Student> students)
        {
            FileStream stream = new FileStream(path, FileMode.Open);
            //获取Excel
            XSSFWorkbook workbook = new XSSFWorkbook(stream);
            //获取文件的sheet
            XSSFSheet worksheet = workbook.GetSheet("Sheet1") as XSSFSheet;
            //开始逐行导入Excel的数据
            for (int i = 1; i < worksheet.LastRowNum; i++)
            {
                if (i == 0)
                    continue;
                Student stu = new Student();
                IRow row = worksheet.GetRow(i);
                for (int j = 0; j < row.LastCellNum; j++)
                {

                    ICell cell = row.GetCell(j);
                    switch (j)
                    {
                        case 0:
                            {
                                stu.Name = Convert.ToString(cell);
                                break;
                            }
                        case 1:
                            {
                                stu.Number = Convert.ToString(cell);
                                break;
                            }
                        case 2:
                            {
                                stu.Mobile = Convert.ToString(cell);
                                break;
                            }
                        case 3:
                            {
                                string s = Convert.ToString(cell);
                                stu.Grade = Convert.ToInt32(s);
                                break;
                            }
                        case 4:
                            {
                                stu.Birth = DateTime.Parse(Convert.ToString(cell));
                                break;
                            }
                    }

                }
                students.Add(stu);
            }
        }
    }
}
